{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Notebook for Ex-Post Analysis of linear Models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "mk, 16.7.2021"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Packages and Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn_coefs = \"../results/table_coefficients/df_coefs_lags12.csv\"\n",
    "fn_gen_agg = \"../data/generation_aggregated.csv\"\n",
    "fn_cost = \"../data/RES_Subsidies_DE_and_UK.xlsx\"\n",
    "fn_out = \"../results/results_linear_submission.xlsx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_covs = \"../results/cov_for_SE/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Choose options**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose phi tilde - needs to be adusted for ex-post calculations of models phi_07_12 and phi_08_12\n",
    "phi_tilde = 0.74\n",
    "\n",
    "# Choose models\n",
    "Models = [\"linear\", \"linear_12\", \"hourFE_12\", \"mofyFE_12\", \"monthFE_12\", \"phi_07_12\", \"phi_08_12\"]\n",
    "\n",
    "# Dummy for linear specification\n",
    "L = \"yes\" \n",
    "\n",
    "# Only use significant coefficients\n",
    "significance = \"yes\" \n",
    "\n",
    "# Start and end date\n",
    "tstart = \"1.1.2015\"\n",
    "tend = \"29.12.2016\"\n",
    "\n",
    "# Define regimes and technologies\n",
    "regimes = [\"low\", \"high\"]\n",
    "res = [\"wind\", \"solar\"]\n",
    "tech = [\"coal\",\"ccgt\"]\n",
    "\n",
    "#  Estimates for Social Cost of Carbon and Levelized Cost of Electricity\n",
    "scc = 50 # €/t\n",
    "lcoe = {'RE': [\"wind\", \"solar\"], 'lcoe': [100,100]} # €/MWh\n",
    "df_lcoe = pd.DataFrame(data=lcoe)\n",
    "\n",
    "# Carbon coefficient per MWh electricity generated\n",
    "d = {'tech': [\"coal\",\"ccgt\"], 'e': [0.89,0.38]} # t CO2 per MWh_el\n",
    "df_e = pd.DataFrame(data=d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load and prepare data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get estimated coefficients as well as input data used for estimations.\n",
    "df_coefs_in = pd.read_csv(fn_coefs)\n",
    "df_coefs_in = df_coefs_in[df_coefs_in.Model.isin(Models)]\n",
    "df_gen_agg = pd.read_csv(fn_gen_agg, parse_dates=True, index_col=\"date\")[tstart:tend].copy()\n",
    "df_cost = pd.read_excel(fn_cost, sheet_name=\"support\")\n",
    "\n",
    "# Covariance Matrix\n",
    "covs = {}\n",
    "for Model in Models:\n",
    "    for t in tech:\n",
    "        df_cov = pd.read_csv(dir_covs + \"%s_fpn_%s_cov.csv\" % (Model, t))\n",
    "        df_cov = df_cov.set_index(\"index\")\n",
    "        df_cov.index.names = [None] \n",
    "        covs.update( {\"%s_fpn_%s\" % (Model, t) : df_cov} )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare data frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_results_ import get_hourly_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>demand</th>\n",
       "      <th>wind</th>\n",
       "      <th>solar</th>\n",
       "      <th>w_wind</th>\n",
       "      <th>w_solar</th>\n",
       "      <th>w_demand</th>\n",
       "      <th>pele_uk</th>\n",
       "      <th>pcarb</th>\n",
       "      <th>subsidy_wind_paid</th>\n",
       "      <th>subsidy_solar_paid</th>\n",
       "      <th>subsidy_wind_mwh</th>\n",
       "      <th>subsidy_solar_mwh</th>\n",
       "      <th>regime</th>\n",
       "      <th>wsubsidy_wind_mwh</th>\n",
       "      <th>wsubsidy_solar_mwh</th>\n",
       "      <th>wpele_wind_mwh</th>\n",
       "      <th>wpele_solar_mwh</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-01 00:00:00</th>\n",
       "      <td>17791.516667</td>\n",
       "      <td>6989.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000046</td>\n",
       "      <td>39.61</td>\n",
       "      <td>19.150881</td>\n",
       "      <td>472192.394807</td>\n",
       "      <td>0.0</td>\n",
       "      <td>67.557392</td>\n",
       "      <td>155.19</td>\n",
       "      <td>low</td>\n",
       "      <td>0.020344</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011928</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-01 01:00:00</th>\n",
       "      <td>17489.516667</td>\n",
       "      <td>6971.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000045</td>\n",
       "      <td>37.18</td>\n",
       "      <td>19.150881</td>\n",
       "      <td>470942.583046</td>\n",
       "      <td>0.0</td>\n",
       "      <td>67.557392</td>\n",
       "      <td>155.19</td>\n",
       "      <td>low</td>\n",
       "      <td>0.020291</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011167</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           demand    wind  solar    w_wind  w_solar  w_demand  \\\n",
       "date                                                                            \n",
       "2015-01-01 00:00:00  17791.516667  6989.5    0.0  0.000111      0.0  0.000046   \n",
       "2015-01-01 01:00:00  17489.516667  6971.0    0.0  0.000111      0.0  0.000045   \n",
       "\n",
       "                     pele_uk      pcarb  subsidy_wind_paid  \\\n",
       "date                                                         \n",
       "2015-01-01 00:00:00    39.61  19.150881      472192.394807   \n",
       "2015-01-01 01:00:00    37.18  19.150881      470942.583046   \n",
       "\n",
       "                     subsidy_solar_paid  subsidy_wind_mwh  subsidy_solar_mwh  \\\n",
       "date                                                                           \n",
       "2015-01-01 00:00:00                 0.0         67.557392             155.19   \n",
       "2015-01-01 01:00:00                 0.0         67.557392             155.19   \n",
       "\n",
       "                    regime  wsubsidy_wind_mwh  wsubsidy_solar_mwh  \\\n",
       "date                                                                \n",
       "2015-01-01 00:00:00    low           0.020344                 0.0   \n",
       "2015-01-01 01:00:00    low           0.020291                 0.0   \n",
       "\n",
       "                     wpele_wind_mwh  wpele_solar_mwh  \n",
       "date                                                  \n",
       "2015-01-01 00:00:00        0.011928              0.0  \n",
       "2015-01-01 01:00:00        0.011167              0.0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_dem = get_hourly_df(df_gen_agg, phi_tilde, df_cost, regimes, res)\n",
    "df_dem.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extract estimation results and calculate generation offset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare dataframe to calculate replacement effects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Extract and rename RE-coefficients to calcuate average marginal replacement effect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_coefs = {\"low_wind\": \"beta1\", \"low_solar\": \"beta1\",\n",
    "             \"high_wind\": \"beta1\", \"high_solar\": \"beta1\",\n",
    "             \"low_wind_wind\": \"beta2\", \"low_solar_solar\": \"beta2\",\n",
    "             \"high_wind_wind\": \"beta2\", \"high_solar_solar\": \"beta2\",\n",
    "             \"low_wind_wind_wind\": \"beta3\", \"low_solar_solar_solar\": \"beta3\",\n",
    "             \"high_wind_wind_wind\": \"beta3\", \"high_solar_solar_solar\": \"beta3\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tech</th>\n",
       "      <th>RE</th>\n",
       "      <th>regime</th>\n",
       "      <th>Model</th>\n",
       "      <th>beta1</th>\n",
       "      <th>demand</th>\n",
       "      <th>solar</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>hourFE_12</td>\n",
       "      <td>-0.673425</td>\n",
       "      <td>21814.936489</td>\n",
       "      <td>973.502201</td>\n",
       "      <td>3699.261354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>linear</td>\n",
       "      <td>-0.549900</td>\n",
       "      <td>21814.936489</td>\n",
       "      <td>973.502201</td>\n",
       "      <td>3699.261354</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   tech     RE regime      Model     beta1        demand       solar  \\\n",
       "0  ccgt  solar   high  hourFE_12 -0.673425  21814.936489  973.502201   \n",
       "1  ccgt  solar   high     linear -0.549900  21814.936489  973.502201   \n",
       "\n",
       "          wind  \n",
       "0  3699.261354  \n",
       "1  3699.261354  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We extract coefficients and standard errors.\n",
    "df_coefs = df_coefs_in.copy()\n",
    "df_coefs.tech = df_coefs.tech.map(lambda x: x.split(\"_\")[-1])\n",
    "df_coefs[\"regime\"] = df_coefs.variable.map(lambda x: x.split(\"_\")[0])\n",
    "df_coefs[\"RE\"] = df_coefs.variable.map(lambda x: x.split(\"_\")[1])\n",
    "\n",
    "# Rename coefficients\n",
    "df_ = df_coefs.copy()\n",
    "df_ = df_[df_.variable.isin(map_coefs.keys())].copy()\n",
    "df_[\"beta\"] = df_.variable.map(map_coefs)\n",
    "df_ = df_.drop(\"variable\", axis=1)\n",
    "cols = [\"beta\", \"tech\", \"RE\", \"regime\", \"Model\"]\n",
    "value_vars = [\"coef\", \"std\", \"pvalue\"]\n",
    "\n",
    "if significance == \"yes\":\n",
    "    df_.loc[df_.pvalue>0.1, \"coef\"] = 0\n",
    "\n",
    "df_ = pd.melt(df_, id_vars=cols, value_vars=value_vars)\n",
    "df_ = df_[df_.variable == \"coef\"\n",
    "                ].drop(\"variable\", axis=1\n",
    "                      ).pivot_table(\"value\", [\"tech\", \"RE\", \"regime\", \"Model\"], \"beta\").reset_index()\n",
    "\n",
    "# Add mean values of wind, solar and demand per regime to calculate replacement effects\n",
    "df_2 = df_.copy()\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).demand.mean(), on=\"regime\")\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).solar.mean(), on=\"regime\")\n",
    "df_2 = df_2.merge(df_dem.groupby(\"regime\", as_index=False).wind.mean(), on=\"regime\")\n",
    "df_2.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function needs to be adapted for other specifications!\n",
    "def get_standard_error_linear(Model, tech, demand, regime, RE, gen_RE):\n",
    "    \"\"\"function to calculate standard errors of marginal effects evaluated at mean demand (\"Delta_q\")\n",
    "    :param Model: <string> with Model name\n",
    "    :param tech: <string> with fossil technology\n",
    "    :param demand_mean: mean demand\n",
    "    :param regime: <string> with regime\n",
    "    :param RE: <string> with renewable energy technology\n",
    "    :return se: <float> with regression results\"\"\"\n",
    "    \n",
    "    df_cov = covs[\"%s_fpn_%s\" % (Model, tech)]\n",
    "    coefs = [\"%s_%s\" % (regime, RE)]\n",
    "    weights = pd.Series(index=coefs, data = [1])\n",
    "    weights_m = weights.values\n",
    "    cov = df_cov.loc[coefs, coefs].values\n",
    "    se = np.sqrt(weights_m.dot(cov).dot(weights_m))\n",
    "    \n",
    "    return se"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_df_Q_linear(df, RE):\n",
    "    df_ = df.copy()\n",
    "\n",
    "    df_RE = df_.loc[df_.RE==RE].copy()\n",
    "    df_RE[\"deltaQ\"] = df_RE.beta1\n",
    "    df_RE[\"deltaQ_se\"] = df_RE.apply(lambda row: get_standard_error_linear(\n",
    "        row[\"Model\"], row[\"tech\"], row[\"demand\"], row[\"regime\"], row[\"RE\"], row[RE]), axis=1)\n",
    "\n",
    "    return df_RE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate generation results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "df_RE[\"deltaQ\"] = df_RE.beta1 + 2*df_RE.beta2*df_RE[RE] + df_RE.beta3*df_RE.demand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tech</th>\n",
       "      <th>RE</th>\n",
       "      <th>Model</th>\n",
       "      <th>regime</th>\n",
       "      <th>deltaQ</th>\n",
       "      <th>deltaQ_se</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>wind</td>\n",
       "      <td>hourFE_12</td>\n",
       "      <td>high</td>\n",
       "      <td>-0.769612</td>\n",
       "      <td>0.014239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>ccgt</td>\n",
       "      <td>wind</td>\n",
       "      <td>linear</td>\n",
       "      <td>high</td>\n",
       "      <td>-0.768269</td>\n",
       "      <td>0.005228</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   tech    RE      Model regime    deltaQ  deltaQ_se\n",
       "7  ccgt  wind  hourFE_12   high -0.769612   0.014239\n",
       "8  ccgt  wind     linear   high -0.768269   0.005228"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if L == \"yes\":\n",
    "    RE = \"wind\"\n",
    "    df_Q_wind = get_df_Q_linear(df_2, RE)\n",
    "    RE = \"solar\"\n",
    "    df_Q_solar = get_df_Q_linear(df_2, RE)\n",
    "    df_Q = pd.concat([df_Q_wind,df_Q_solar])\n",
    "    \n",
    "else: \n",
    "    RE = \"wind\"\n",
    "    df_Q_wind = get_df_Q(df_2, RE)\n",
    "    RE = \"solar\"\n",
    "    df_Q_solar = get_df_Q(df_2, RE)\n",
    "    df_Q = pd.concat([df_Q_wind,df_Q_solar])\n",
    "\n",
    "df_Q = df_Q[[\"tech\", \"RE\", \"Model\", \"regime\", \"deltaQ\", \"deltaQ_se\"]]\n",
    "df_Q.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate emission offset and cost data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_results_ import get_final_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RE</th>\n",
       "      <th>regime</th>\n",
       "      <th>Model</th>\n",
       "      <th>deltaE</th>\n",
       "      <th>deltaE_se</th>\n",
       "      <th>pcarb</th>\n",
       "      <th>market_carbon_rent</th>\n",
       "      <th>market_carbon_rent_se</th>\n",
       "      <th>meb</th>\n",
       "      <th>meb_se</th>\n",
       "      <th>premium</th>\n",
       "      <th>premium_se</th>\n",
       "      <th>market_income</th>\n",
       "      <th>operating_cost</th>\n",
       "      <th>feed_in</th>\n",
       "      <th>premium_paid</th>\n",
       "      <th>premium_paid_t</th>\n",
       "      <th>lcoe</th>\n",
       "      <th>wpele</th>\n",
       "      <th>MAC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>hourFE_12</td>\n",
       "      <td>-0.546553</td>\n",
       "      <td>0.016954</td>\n",
       "      <td>28.534708</td>\n",
       "      <td>15.595734</td>\n",
       "      <td>0.483771</td>\n",
       "      <td>27.327657</td>\n",
       "      <td>0.847688</td>\n",
       "      <td>11.731923</td>\n",
       "      <td>0.696090</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>22.753532</td>\n",
       "      <td>50.081189</td>\n",
       "      <td>62.577734</td>\n",
       "      <td>114.495240</td>\n",
       "      <td>100</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>112.799156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>solar</td>\n",
       "      <td>high</td>\n",
       "      <td>linear</td>\n",
       "      <td>-0.609551</td>\n",
       "      <td>0.007086</td>\n",
       "      <td>28.534708</td>\n",
       "      <td>17.393355</td>\n",
       "      <td>0.202209</td>\n",
       "      <td>30.477542</td>\n",
       "      <td>0.354321</td>\n",
       "      <td>13.084187</td>\n",
       "      <td>0.290955</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>20.955911</td>\n",
       "      <td>51.433453</td>\n",
       "      <td>62.577734</td>\n",
       "      <td>102.662044</td>\n",
       "      <td>100</td>\n",
       "      <td>38.349266</td>\n",
       "      <td>101.141251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      RE regime      Model    deltaE  deltaE_se      pcarb  \\\n",
       "0  solar   high  hourFE_12 -0.546553   0.016954  28.534708   \n",
       "1  solar   high     linear -0.609551   0.007086  28.534708   \n",
       "\n",
       "   market_carbon_rent  market_carbon_rent_se        meb    meb_se    premium  \\\n",
       "0           15.595734               0.483771  27.327657  0.847688  11.731923   \n",
       "1           17.393355               0.202209  30.477542  0.354321  13.084187   \n",
       "\n",
       "   premium_se  market_income  operating_cost    feed_in  premium_paid  \\\n",
       "0    0.696090      38.349266       22.753532  50.081189     62.577734   \n",
       "1    0.290955      38.349266       20.955911  51.433453     62.577734   \n",
       "\n",
       "   premium_paid_t  lcoe      wpele         MAC  \n",
       "0      114.495240   100  38.349266  112.799156  \n",
       "1      102.662044   100  38.349266  101.141251  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_deltaE = get_final_results(df_Q, df_dem, df_e, regimes, res, scc, df_lcoe)\n",
    "df_deltaE.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "wxls = pd.ExcelWriter(fn_out)\n",
    "df_deltaE.melt(id_vars=[\"RE\", \"regime\", \"Model\"]).to_excel(wxls, index=False, sheet_name=\"coefs_Delta_E\")\n",
    "df_Q.melt(id_vars=[\"RE\", \"regime\", \"Model\", \"tech\"]).to_excel(wxls, index=False, sheet_name=\"coefs_Delta_Q\")\n",
    "wxls.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
